// pages/payment/payment.js

var app = getApp()
Page({
  data: {
    total_fee:0,
    fromPass:'',
    course: [],
    payBtnDisabled:false,
    payBtnLabel: '立即支付',
    headimg: [
      { url: '../imgs/swiper/swp1.jpg' },
      { url: '../imgs/swiper/swp2.jpg' },
      { url: '../imgs/swiper/swp3.jpg' }
    ],

items: [
      { id: 0, hidden:false, name: 'shuxue', value: '-初中数学 ',isChecked: false},
      { id: 1, hidden: false, name: 'wuli', value: '-初中物理 ', isChecked: false },
      { id: 2,hidden:false, name: 'huaxue', value: '-初中化学 ', isChecked:false }
    ]
   
  },
  
//刷新点击后的金额
  toggleItem:function(e){
     //控制单门课程的选中与反选      
      let gid=e.target.dataset.id-1; 
      console.log('id:'+gid);
      console.log('name:' + this.data.course[gid].value);
    if (this.data.course[gid].isChecked){
        this.data.course[gid].isChecked=false;
    } else {
      this.data.course[gid].isChecked=true;
    }    
     
    if (this.data.course[gid].isChecked){
      if (gid == 0) { app.globalData.member_tb_shuxue = 1; }
      if (gid == 1) { app.globalData.member_tb_wuli = 1; }
      if (gid == 2) { app.globalData.member_tb_huaxue = 1; }

      if (gid == 0) { app.globalData.member_zh_shuxue=1;}
      if (gid == 1) { app.globalData.member_zh_wuli = 1; }
      if (gid == 2) { app.globalData.member_zh_huaxue = 1; }
      
      this.setData({
        total_fee: this.data.total_fee + parseInt(this.data.course[gid].price)
      })

      } else {
      if (gid == 0) { app.globalData.member_tb_shuxue = 0; }
      if (gid == 1) { app.globalData.member_tb_wuli = 0; }
      if (gid == 2) { app.globalData.member_tb_huaxue = 0; }

      if (gid == 0) { app.globalData.member_zh_shuxue = 0; }
      if (gid == 1) { app.globalData.member_zh_wuli = 0; }
      if (gid == 2) { app.globalData.member_zh_huaxue = 0; }
   
      this.setData({
        total_fee:this.data.total_fee - parseInt(this.data.course[gid].price)
      })
     
      } 

  //console.log('shuxue:'+app.globalData.member_shuxue);
  //console.log('wuli:'+app.globalData.member_wuli);
  //console.log('huaxue:'+app.globalData.member_huaxue)
  //console.log('总价：' + this.data.total_fee)
  
  wx.setStorageSync("total_fee", this.data.total_fee)
    //同步获取数据

  },

//使用login()获取的code(临时登录凭证），并发送致后台
//后台，使用code,appid,appsecret换取openid

myPayment:function(){
  if(this.data.total_fee=0){
    wx.showToast({
      title: '至少需要选一门课！',
      icon: 'none',
      duration: 2000
    })     

  } else {

  var that = this 
  wx.login({
    success: res => {
        // console.log('code:'+res.code)
        //发送请求      
       that.getOpenId(res.code)  
       
    }
  })    
 }
 
  var that = this;
  var num = 30;
  var timer = setInterval(function () {
    num--;
    if (num <= 0) {
      clearInterval(timer);
      that.setData({
        payBtnLabel: '立即支付',
        payBtnDisabled: false,
      })
    }
    else {
      that.setData({
        payBtnLabel: num + 's',
        payBtnDisabled: true
      })
    }
  }, 1000)

},

//获到openId
getOpenId:function(code,total_fee){ 
  let fee = wx.getStorageSync('total_fee')
  var that = this
  wx.request({
    url: 'https://www.beicho.com/get_openId.php', //接口地址 
    method: 'GET',
    header: {
      'content-type': 'application/json'
    },
    data: {
      code: code
    },
    success: res => {      
     /**获到openid成功，下面调用统一下单接口，实现预支 */     
     that.get_prePayId(res.data.openid,fee)    
      /**预支付结束 */
    }
  })
},

//统一下单，获到prePayId
  get_prePayId: function (openid, total_fee) {
    var that = this
      wx.request({
      url: 'https://www.beicho.com/get_prePayId.php',
      data: {
        openid: openid,
        total_fee:total_fee
      },
      success: res => {
        //console.log('res:'+res.data)  
        that.wxPay(res.data)  //调用支付
        
      }
    })
 },

//实际完成支付
wxPay:function(data){
  // data是服务端返回的JSON
  // 加上success、fail回调后，正好符合wx.requestPayment()参数的格式
  var that=this
  wx.requestPayment({
    timeStamp: data.timeStamp,
    nonceStr: data.nonceStr,
    package: data.package,
    signType: 'MD5',
    paySign: data.paySign,
    
    success: res => {
      //console.log('支付成功',res)      
     that.save_memberShap(
        app.globalData.openid,
        app.globalData.member_tb_shuxue,
        app.globalData.member_tb_wuli,
        app.globalData.member_tb_huaxue,

        app.globalData.member_zh_shuxue,
        app.globalData.member_zh_wuli,
        app.globalData.member_zh_huaxue       
        
      );  //此处把会员购买的课程放入数据库中
    
     },
    fail:res=> { 
     // 用户支付失败后的代码
      wx.showToast({
        title: '抱歉，支付失败！',
        icon: 'none',
        duration: 3000
      })
    }
  })     
  
},

//保存会员购买的课程信息
  save_memberShap: function (openid, tb_shuxue, tb_wuli, tb_huaxue,zh_shuxue, zh_wuli, zh_huaxue) {
    var that = this
    wx.request({
      url: 'https://www.beicho.com/save_memberShap.php', //接口地址 
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      data: {
        openid:openid,
        tb_shuxue:tb_shuxue,
        tb_wuli: tb_wuli,
        tb_huaxue:tb_huaxue,
        zh_shuxue: zh_shuxue,
        zh_wuli: zh_wuli,
        zh_huaxue: zh_huaxue

      },
      success: res => {
        // 用户支付成功后的代码
        wx.showToast({
          title: '支付成功！',
          icon: 'none',
          duration: 3000
        })
        // that.reback()    //处理回调函数的支付通知
        that.setData({
          btn_disabled: false
        })

        that.reBack_person() //返回个人中心    
      }
    })
  },


//返回个人中心
reback:function(){
  var that=this
  wx.request({
    url: 'https://www.beicho.com/pay_notify.php',
    data: {

    },
    success: res => {
      //console.log(res.data)
      wx.showToast({
        title:'成功回调！',
        icon: 'none',
        duration: 5000
      })
      that.setData({
        btn_disabled: false
      })
     
      that.reBack_from() //返回个人中心
    }
  })
},


reBack_from:function(){
  if(this.fromPass=='tab'){
    wx.switchTab({
      url: '../person/person'
    })

  } else {
    wx.navigateBack()
  }

},


//获取用户信息
get_courseInfo:function(){
  //读到课程信息
  var that=this
  wx.request({
    url: 'https://www.beicho.com/get_courseInfo.php', //接口地址 
    method: 'GET',
    header: {
      'content-type': 'application/json'
    },
    data: {},
    success: res => {   
       that.setData({
          course:res.data
       })
            
      if(app.globalData.member_tb_shuxue== 1){
       this.setData({
         'course[0].hidden':1
       })
       // console.log('数学:' + this.data.course[0].hidden);
       // console.log('数学:' + app.globalData.member_shuxue);
      }

      if (app.globalData.member_tb_wuli == 1) {
        this.setData({
          'course[1].hidden': 1
        })
      //  console.log('数学:' + this.data.course[1].hidden);
      //  console.log('物理:' + app.globalData.member_wuli);
      }
      if (app.globalData.member_tb_huaxue == 1) {
        this.setData({
          'course[2].hidden': 1
        })
      //  console.log('数学:' + this.data.course[2].hidden);
       // console.log('化学:' + app.globalData.member_huaxue);
      }

      if (app.globalData.member_zh_shuxue == 1) {
        this.setData({
          'course[3].hidden': 1
        })
        //  console.log('数学:' + this.data.course[2].hidden);
        // console.log('化学:' + app.globalData.member_huaxue);
      }

      if (app.globalData.member_zh_wuli == 1) {
        this.setData({
          'course[4].hidden': 1
        })
        //  console.log('数学:' + this.data.course[2].hidden);
        // console.log('化学:' + app.globalData.member_huaxue);
      }
      if (app.globalData.member_zh_huaxue == 1) {
        this.setData({
          'course[5].hidden': 1
        })
        //  console.log('数学:' + this.data.course[2].hidden);
        // console.log('化学:' + app.globalData.member_huaxue);
      }

     // console.log("数据course:",that.data.course)

    }
  })
 //读取课程信息结束

},



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {    
     this.get_courseInfo()
     this.setData({
        fromPass:options.fromPass
     })


  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
         total_fee:0
    })
  },

fresh_arryCourse:function(){
   //修改数学同步课堂状态
  if (app.globalData.member_tb_shuxue == 1) {
    this.setData({
      'items[0].isChecked': true,
      'items[0].hidden': true
    })
  } else {
    this.setData({
      'items[0].isChecked': false,
      'items[0].hidden': false
    })
  }


  //修改当前用户物理课程的会员状态
  if (app.globalData.member_tb_wuli == 1) {
    this.setData({
      'items[1].isChecked': true,
      'items[1].hidden': true
    })

  } else {
    this.setData({
      'items[1].isChecked': false,
      'items[1].hidden': false

    })
  }


  //修改当前用户化学课程的会员状态
  if (app.globalData.member_tb_huaxue == 1) {
    this.setData({
      'items[2].isChecked': true,
      'items[2].hidden': true

    })
  } else {
    this.setData({
      'items[2].isChecked': false,
      'items[2].hidden': false

    })
  }

  //修改数学综合课堂状态
  if (app.globalData.member_zh_shuxue == 1) {
    this.setData({
      'items[3].isChecked': true,
      'items[3].hidden': true
    })
  } else {
    this.setData({
      'items[3].isChecked': false,
      'items[3].hidden': false
    })
  }


  //修改当前用户物理课程的会员状态
  if (app.globalData.member_zh_wuli == 1) {
    this.setData({
      'items[4].isChecked': true,
      'items[4].hidden': true
    })

  } else {
    this.setData({
      'items[4].isChecked': false,
      'items[4].hidden': false

    })
  }


  //修改当前用户化学课程的会员状态
  if (app.globalData.member_zh_huaxue == 1) {
    this.setData({
      'items[5].isChecked': true,
      'items[5].hidden': true

    })
  } else {
    this.setData({
      'items[5].isChecked': false,
      'items[5].hidden': false
    })
  }
},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {  
    
    //修改当前用户数学课程的会员状态
    this.fresh_arryCourse;
 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
  
})